class LotteryGame {
    constructor() {
        this.frontNumbers = new Set();
        this.backNumbers = new Set();
        this.history = [];
        this.initUI();
        this.bindEvents();
    }

    initUI() {
        // 生成前区号码
        const frontArea = document.querySelector('.front-area');
        for (let i = 1; i <= 35; i++) {
            const num = document.createElement('div');
            num.className = 'number';
            num.textContent = i.toString().padStart(2, '0');
            frontArea.appendChild(num);
        }

        // 生成后区号码
        const backArea = document.querySelector('.back-area');
        for (let i = 1; i <= 12; i++) {
            const num = document.createElement('div');
            num.className = 'number';
            num.textContent = i.toString().padStart(2, '0');
            backArea.appendChild(num);
        }
    }

    bindEvents() {
        // 前区选号
        document.querySelector('.front-area').addEventListener('click', (e) => {
            if (e.target.classList.contains('number')) {
                if (e.target.classList.contains('selected')) {
                    e.target.classList.remove('selected');
                    this.frontNumbers.delete(parseInt(e.target.textContent));
                } else if (this.frontNumbers.size < 5) {
                    e.target.classList.add('selected');
                    this.frontNumbers.add(parseInt(e.target.textContent));
                }
            }
        });

        // 后区选号
        document.querySelector('.back-area').addEventListener('click', (e) => {
            if (e.target.classList.contains('number')) {
                if (e.target.classList.contains('selected')) {
                    e.target.classList.remove('selected');
                    this.backNumbers.delete(parseInt(e.target.textContent));
                } else if (this.backNumbers.size < 2) {
                    e.target.classList.add('selected');
                    this.backNumbers.add(parseInt(e.target.textContent));
                }
            }
        });

        // 机选按钮
        document.getElementById('randomBtn').addEventListener('click', () => this.randomSelect());

        // 清空按钮
        document.getElementById('clearBtn').addEventListener('click', () => this.clearSelection());

        // 开奖按钮
        document.getElementById('drawBtn').addEventListener('click', () => this.draw());
    }

    randomSelect() {
        this.clearSelection();
        
        // 随机选择前区号码
        while (this.frontNumbers.size < 5) {
            const num = Math.floor(Math.random() * 35) + 1;
            this.frontNumbers.add(num);
        }

        // 随机选择后区号码
        while (this.backNumbers.size < 2) {
            const num = Math.floor(Math.random() * 12) + 1;
            this.backNumbers.add(num);
        }

        // 更新UI
        this.updateSelectionUI();
    }

    clearSelection() {
        this.frontNumbers.clear();
        this.backNumbers.clear();
        document.querySelectorAll('.number').forEach(num => num.classList.remove('selected'));
    }

    updateSelectionUI() {
        document.querySelectorAll('.front-area .number').forEach(num => {
            const number = parseInt(num.textContent);
            if (this.frontNumbers.has(number)) {
                num.classList.add('selected');
            }
        });

        document.querySelectorAll('.back-area .number').forEach(num => {
            const number = parseInt(num.textContent);
            if (this.backNumbers.has(number)) {
                num.classList.add('selected');
            }
        });
    }

    draw() {
        if (this.frontNumbers.size !== 5 || this.backNumbers.size !== 2) {
            alert('请选择5个前区号码和2个后区号码！');
            return;
        }

        // 生成开奖号码
        const drawFront = new Set();
        const drawBack = new Set();

        while (drawFront.size < 5) {
            drawFront.add(Math.floor(Math.random() * 35) + 1);
        }
        while (drawBack.size < 2) {
            drawBack.add(Math.floor(Math.random() * 12) + 1);
        }

        // 计算中奖情况
        const frontMatch = [...this.frontNumbers].filter(x => drawFront.has(x)).length;
        const backMatch = [...this.backNumbers].filter(x => drawBack.has(x)).length;

        // 显示开奖结果
        this.showDrawResult([...drawFront].sort((a,b)=>a-b), [...drawBack].sort((a,b)=>a-b), frontMatch, backMatch);

        // 添加到历史记录
        this.addToHistory([...this.frontNumbers], [...this.backNumbers], frontMatch, backMatch);
    }

    showDrawResult(drawFront, drawBack, frontMatch, backMatch) {
        const frontDrawDiv = document.querySelector('.front-draw');
        const backDrawDiv = document.querySelector('.back-draw');
        const prizeResultDiv = document.querySelector('.prize-result');

        frontDrawDiv.innerHTML = drawFront.map(n => `<div class="draw-number">${n.toString().padStart(2, '0')}</div>`).join('');
        backDrawDiv.innerHTML = drawBack.map(n => `<div class="draw-number">${n.toString().padStart(2, '0')}</div>`).join('');

        const prize = this.calculatePrize(frontMatch, backMatch);
        prizeResultDiv.textContent = prize;
    }

    calculatePrize(frontMatch, backMatch) {
        if (frontMatch === 5 && backMatch === 2) return '恭喜您中得一等奖！';
        if (frontMatch === 5 && backMatch === 1) return '恭喜您中得二等奖！';
        if (frontMatch === 5 && backMatch === 0) return '恭喜您中得三等奖！';
        if (frontMatch === 4 && backMatch === 2) return '恭喜您中得四等奖！';
        if (frontMatch === 4 && backMatch === 1) return '恭喜您中得五等奖！';
        if (frontMatch === 3 && backMatch === 2) return '恭喜您中得六等奖！';
        if (frontMatch === 4 && backMatch === 0) return '恭喜您中得七等奖！';
        if (frontMatch === 3 && backMatch === 1) return '恭喜您中得八等奖！';
        if (frontMatch === 2 && backMatch === 2) return '恭喜您中得九等奖！';
        return '很遗憾，未中奖';
    }

    addToHistory(frontNums, backNums, frontMatch, backMatch) {
        const historyItem = {
            frontNumbers: frontNums,
            backNumbers: backNums,
            prize: this.calculatePrize(frontMatch, backMatch),
            timestamp: new Date().toLocaleString()
        };

        this.history.unshift(historyItem);
        if (this.history.length > 10) this.history.pop();

        this.updateHistoryUI();
    }

    updateHistoryUI() {
        const historyList = document.querySelector('.history-list');
        historyList.innerHTML = this.history.map(item => `
            <div class="history-item">
                <div class="history-numbers">
                    <span>${item.frontNumbers.map(n => n.toString().padStart(2, '0')).join(' ')}</span>
                    <span>|</span>
                    <span>${item.backNumbers.map(n => n.toString().padStart(2, '0')).join(' ')}</span>
                </div>
                <div class="history-prize">${item.prize}</div>
            </div>
        `).join('');
    }
}

// 初始化游戏
new LotteryGame(); 